﻿using CefSharp;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace CodeGenerateEngine.Serv
{
    internal abstract class DataService
    {

        protected static IResourceHandler JsonContent(object data, Newtonsoft.Json.JsonSerializerSettings jsonSerializerSettings = null)
        {
            var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data, jsonSerializerSettings));
            var result = ResourceHandler.FromStream(new MemoryStream(bytes), "application/json");
            result.StatusCode = 200;
            return result;
        }

        protected static IResourceHandler JsonContent<T>(T data, Newtonsoft.Json.JsonSerializerSettings jsonSerializerSettings = null)
        {
            var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data, jsonSerializerSettings));
            var result = ResourceHandler.FromStream(new MemoryStream(bytes), "application/json");
            result.StatusCode = 200;
            return result;
        }

        protected static IResourceHandler TextContent(string text)
        {
            var bytes = Encoding.UTF8.GetBytes(text);
            var result = ResourceHandler.FromStream(new MemoryStream(bytes), "text/plain");
            result.StatusCode = 200;
            return result;
        }

        protected static IResourceHandler TextContent(string text, Encoding encoding)
        {
            var bytes = encoding.GetBytes(text);
            var result = ResourceHandler.FromStream(new MemoryStream(bytes), "text/plain");
            result.StatusCode = 200;
            return result;
        }
    }
}
